#include <cctype>
#include <cstdio>
using namespace std;

long long a, b, ind, m;

long long quickpow(long long x, long long index) {
	long long res = 1;
	while (index) {
		if (index & 1) (res *= x) %= m;
		index >>= 1;
		(x *= x) %= m;
	}
	return res;
}

long long phi(long long base) {
	long long res = base;
	for (int i = 2; i * i <= base; ++i) {
		if (base % i == 0) {
			res = res / i * (i - 1);
			while (base % i == 0) base /= i;
		}
	}
	if (base > 1) res = res / base * (base - 1);
	return res;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("Euler.in", "r", stdin);
	freopen("Euler.out", "w", stdout);
#endif
#endif
	scanf("%lld %lld", &a, &m);
	int ch = 0;
	bool flag = false;
	long long ph = phi(m);
	// printf("%lld\n", ph);
	getchar();
	while (isdigit(ch = getchar())) {
		b = (b * 10 + ch - '0');
		if (b >= ph) {
			flag = true;
			b %= ph;
		}
	}
	if (flag) b += ph;
	printf("%lld", quickpow(a, b));
	return 0;
}